﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

public partial class MasterPage : System.Web.UI.MasterPage
{
    private DataSet ds = null;
    private Model.DbSystem dbSystem = DalEx.DbSystemEx.Select(1);
    protected void Page_Load(object sender, EventArgs e)
    {
        hyLogin.Visible = DalEx.DbSystemEx.ReadBaoMingState();
        ReadPaCong();
        if (dbSystem != null && dbSystem.SWebStatus == 0)
        {
            Response.Write(HttpUtility.HtmlDecode(dbSystem.SWebStopAlter));
            Response.End();
        }
        SetBasicInfo();
        if (!Page.IsPostBack)
        {
            string strResult = string.Empty;
            PageEx pageEx = new PageEx();
            if (!DalEx.DbUserDalEx.CheckUserLogin(pageEx.UserType, pageEx.UserId, ref strResult))
            {
                hyLogin.NavigateUrl = "UserLogin.aspx";
            }
            else
            {
                hyLogin.NavigateUrl = "MemberCenter.aspx";
                hyLogin.Text = "会员中心";
            }

            if (dbSystem != null)
            {
                if (!string.IsNullOrEmpty(dbSystem.WeChat))
                {
                    hfWechat.NavigateUrl = dbSystem.WeChat;
                    hfWechat.Attributes.Add("title", "官方微信");
                }
                if (!string.IsNullOrEmpty(dbSystem.Weibo))
                {
                    hfWeibo.NavigateUrl = dbSystem.Weibo;
                    hfWeibo.Attributes.Add("title", "官方微信");
                }
            }
            txtKeywords.Attributes.Add("placeholder", "关键字");
            string strKeywords = Convert.ToString(Request.QueryString["Keywords"]);
            if (strKeywords != null)
            {
                strKeywords = HttpUtility.UrlDecode(strKeywords);
                txtKeywords.Text = strKeywords;
            }
            DataBinds();
        }
    }

    //爬虫处理
    private void ReadPaCong()
    {
        string strIP = WebBase.GetClientIP.ClientIP();
        string[] strArrIPS = strIP.Split('.');
        DataSet temp_ds = Dal.ServerIPDal.Select("IP like '%" + strArrIPS[0] + "." + strArrIPS[1] + "." + strArrIPS[2] + "." + "*%'");
        Model.ServerIP model = new Model.ServerIP();
        if (temp_ds.Tables[0].Rows.Count > 0)
        {
            model = new Model.ServerIP(temp_ds.Tables[0].Rows[0]);
            if (model.StateR == 0)
            {
                if (model.LastTime.ToString("yyyy-MM-dd") != DateTime.Now.ToString("yyyy-MM-dd"))
                {
                    model.LastTime = DateTime.Now;
                    Dal.ServerIPDal.Update(model);
                }
                Response.Redirect("/a/404.aspx");
            }
            else
            {
                if (model.LastTime.ToString("yyyy-MM-dd") != DateTime.Now.ToString("yyyy-MM-dd"))
                {
                    model.LastTime = DateTime.Now;
                    Dal.ServerIPDal.Update(model);
                }
            }
        }
        else
        {
            temp_ds = Dal.ServerIPDal.Select("IP like '%" + strIP + "%'");
            if (temp_ds.Tables[0].Rows.Count > 0)
            {
                model = new Model.ServerIP(temp_ds.Tables[0].Rows[0]);
                if (model.StateR == 0)
                {
                    if (model.LastTime.ToString("yyyy-MM-dd") != DateTime.Now.ToString("yyyy-MM-dd"))
                    {
                        model.LastTime = DateTime.Now;
                        Dal.ServerIPDal.Update(model);
                    }
                    Response.Redirect("/a/404.aspx");
                }
                else
                {
                    if (model.LastTime.ToString("yyyy-MM-dd") != DateTime.Now.ToString("yyyy-MM-dd"))
                    {
                        model.LastTime = DateTime.Now;
                        Dal.ServerIPDal.Update(model);
                    }
                }
            }
        }
    }

    private void DataBinds()
    {
        if (dbSystem != null)
        {
            litSubTitle.Text = dbSystem.SWebName;
            litBottom.Text = HttpUtility.HtmlDecode(dbSystem.SBottom);
            setMeta(dbSystem.STitle, dbSystem.SKeywords, dbSystem.SDescription);
            linkHome.NavigateUrl = dbSystem.SWebUrl;
        }
        //新闻菜单绑定
        ds = DalEx.DbClassEx.Select(" and cStatus=1 and cType=0 and cLevel=0 order by cOrder asc,cId desc");
        rptNewClass.DataSource = ds;
        rptNewClass.DataBind();
        //绑定课程体系菜单
        ds = SqlHelper.Select("select * from dbClass where cId in (select distinct cUpId from dbClass where cLevel=1 and cType=2) order by cOrder asc,cId desc"); ;
        rptClass.DataSource = ds;
        rptClass.DataBind();
        //绑定课程体系菜单
        ds = SqlHelper.Select("select a.* from dbClass a left join dbClass b on a.cId=b.cUpId where a.cLevel=0 and b.cId is null and a.cType=2 order by a.cOrder asc,a.cId desc");
        rptClass0.DataSource = ds;
        rptClass0.DataBind();
        //绑定底部课程体系菜单
        ds = DalEx.DbClassEx.Select(" and cStatus=1 and cType=2 and cLevel=0 order by cOrder asc,cId desc");
        rptCourseClass1.DataSource = ds;
        rptCourseClass1.DataBind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string strKeywords = SqlHelper.String(txtKeywords.Text);
        Response.Redirect("~/Search.aspx?Keywords=" + HttpUtility.UrlEncode(strKeywords));
    }
    protected void rptClass_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            Repeater rptSubClass = (Repeater)e.Item.FindControl("rptSubClass");
            DataRowView drv = (DataRowView)e.Item.DataItem;
            ds = DalEx.DbClassEx.Select(" and cStatus=1 and cType=2 and cUpId=" + Convert.ToInt32(drv["cId"]) + " order by cOrder asc,cId desc");
            rptSubClass.DataSource = ds;
            rptSubClass.DataBind();
        }
    }
    protected void rptCourseClass1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            Label lblSplit = (Label)e.Item.FindControl("lblSplit");
            if ((e.Item.ItemIndex + 1) % 4 == 0)
                lblSplit.Text = "<br />";
            else
                lblSplit.Text = "|";
        }
    }
    private void SetBasicInfo()
    {
        if (dbSystem != null)
        {
            HtmlLink iconLink = new HtmlLink();
            iconLink.Href = dbSystem.SIcon;
            iconLink.Attributes.Add("rel", "shortcut icon");
            iconLink.Attributes.Add("type", "image/x-icon");
            Page.Header.Controls.Add(iconLink);
            setMeta(dbSystem.STitle, dbSystem.SKeywords, dbSystem.SDescription);
        }
    }
    private void setMeta(string title, string key, string des)
    {
        HtmlMeta keywords = new HtmlMeta(); // keywords
        HtmlMeta description = new HtmlMeta(); // description
        keywords.Name = "keywords";
        keywords.Content = key;

        Page.Title = title;
        description.Name = "description";
        description.Content = des;
        Page.Header.Controls.Add(keywords);
        Page.Header.Controls.Add(description);
    }
}
